package com.cxs.componentcode.slice;

import com.cxs.componentcode.provider.NewTypeProvider;
import com.cxs.componentcode.provider.NewsListProvider;
import com.cxs.componentcode.ResourceTable;
import com.cxs.componentcode.data.NewsInfo;
import com.cxs.componentcode.utils.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

public class ListContainerSlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00301, "MainAbility");
    private ListContainer selectList = null;
    private ListContainer newsList = null;
    private String[] typeList = null;
    private String[] newsType = null;
    private String[] newsTitle = null;
    List<NewsInfo> totalNews = null;
    List<NewsInfo> selectNews = null;
    NewsListProvider newsListProvider;
    NewTypeProvider newTypeProvider;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_list_container);
        // 取得组件
        initView();
        initProvider();
        setProvider();
        initListener();
    }

    private void initView() {
        selectList = (ListContainer)findComponentById(ResourceTable.Id_selector_list);
        newsList = (ListContainer)findComponentById(ResourceTable.Id_news_container);
    }

    private void initProvider() {
        // 新闻类型列表
        typeList = new String[]{"All","Health", "Finance", "Technology", "Sport", "Internet", "Game"};
        // 初始化新闻类型，向适配器传递数据
        newTypeProvider = new NewTypeProvider(typeList, this);
        newTypeProvider.notifyDataChanged();

        newsType = new String[]{
                "Health", "Finance", "Sport", "Technology", "Health", "Sport", "Technology",  "Internet", "Game", "Health"
        };
        // 新闻标题列表
        newsTitle = new String[]{
                "Best Enterprise Wi-Fi Network Award of the Wireless Broadband Alliance 2020",
                "Openness and Cooperation Facilitate Industry Upgrade",
                "High-voltage super-fast charging is an inevitable trend",
                "Ten Future Trends of Digital Energy",
                "Ascend Helps Industry, Learning, and Research Promote AI Industry Development in the National AI Contest",
                "Enterprise data centers are moving towards autonomous driving network",
                "One optical fiber lights up a green smart room",
                "Trust technology, embrace openness, and share the world prosperity brought by technology",
                "Intelligent Twins Won the Leading Technology Achievement Award at the 7th World Internet Conference",
                "Maximizing the Value of Wireless Networks and Ushering in the Golden Decade of 5G"
        };
        // 全部新闻
        totalNews = new ArrayList<>();
        selectNews = new ArrayList<>();
        for (int i=0; i<newsType.length; i++) {
            NewsInfo newsInfo = new NewsInfo();
            newsInfo.setTitle(newsTitle[i]);
            newsInfo.setType(newsType[i]);
            // 将数组中的数据添加到List中
            totalNews.add(newsInfo);
        }
        selectNews.addAll(totalNews);
        // 初始化新闻标题，向适配器传递数据
        newsListProvider = new NewsListProvider(selectNews, this);
        newsListProvider.notifyDataChanged();
    }

    private void setProvider() {
        selectList.setItemProvider(newTypeProvider);
        newsList.setItemProvider(newsListProvider);
    }

    private void initListener() {
        selectList.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Text selectText = null;
                Component typeText = component.findComponentById(ResourceTable.Id_news_type_text);
                if (typeText instanceof Text) {
                    selectText = (Text)typeText;
                }
                selectNews.clear();
                // 如果点击了 All
                if (i == 0) {
                    selectNews.addAll(totalNews);
                } else {
                    for (NewsInfo info : totalNews) {
                        if (selectText.getText().equals(info.getType())) {
                            selectNews.add(info);
                        }
                    }
                }
                updateListContainer();
            }
        });
        newsList.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Text newInfoText = null;
                Component newInfo = component.findComponentById(ResourceTable.Id_item_news_title);
                if (newInfo instanceof Text) {
                    newInfoText = (Text)newInfo;
                }
                UIUtils.showTip(getContext(), newInfoText.getText());
            }
        });
    }
    private void updateListContainer() {
        newsListProvider.notifyDataChanged();
        selectList.invalidate();
        selectList.scrollToCenter(0);
    }
}
